package calvin.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.slim3.datastore.Datastore;

import calvin.datastore.meta.Rate1hMeta;
import calvin.datastore.meta.Rate5mMeta;
import calvin.datastore.meta.Rate5sMeta;
import calvin.datastore.model.Rate1h;
import calvin.datastore.model.Rate5m;
import calvin.datastore.model.Rate5s;

import com.google.apphosting.api.ApiProxy;
import com.google.apphosting.api.ApiProxy.Environment;

public class RateHelper {
    private static String RATE_URL = "http://fx.dmm.com/market/rate/var/rate.json?";
    public static Set<String> WATCH_SET = null;
    public static String APP_ID = null;
    static {
        WATCH_SET = new HashSet<String>();
        Environment env = ApiProxy.getCurrentEnvironment();
        APP_ID = env.getAppId();
        int index = APP_ID.indexOf("-");
        String cp = APP_ID.substring(index + 1);
        WATCH_SET.add(cp.toUpperCase());
        /**
        WATCH_SET.add("USDJPY");
        WATCH_SET.add("EURJPY");
        WATCH_SET.add("AUDJPY");
        WATCH_SET.add("EURUSD");
        WATCH_SET.add("AUDUSD");
        */
    }
    
    private static final Logger logger = Logger.getLogger(RateHelper.class.getName());
    
    public static String get() {
        BufferedReader in = null;
        try {
            URL url = new URL(RATE_URL + System.currentTimeMillis());
            URLConnection con = url.openConnection();
            in = new BufferedReader(
                new InputStreamReader(
                con.getInputStream()));
            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = in.readLine()) != null) {
                sb.append(line);
            }
            return sb.toString();
        } catch (IOException e) {
            logger.log(Level.SEVERE, "url connection failed.....");
            return null;
        } finally {
            if(in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    logger.log(Level.SEVERE, "failed to close the http connection stream.....");
                }
            }

        }
    }
    
    public static List<Rate5s> getBaseRateFromStore(String cp, int count) {
        Rate5sMeta e = Rate5sMeta.get();
        return Datastore.query(e)
            .filter(e.cp.equal(cp))
            .sort(e.t.desc)
            .limit(count)
            .asList();
    }
    
    public static List<Rate5m> get5mRateFromStore(String cp, int count) {
        Rate5mMeta e = Rate5mMeta.get();
        return Datastore.query(e)
            .filter(e.cp.equal(cp))
            .sort(e.t.desc)
            .limit(count)
            .asList();
    }
    
    public static List<Rate1h> get1hRateFromStore(String cp, int count) {
        Rate1hMeta e = Rate1hMeta.get();
        return Datastore.query(e)
            .filter(e.cp.equal(cp))
            .sort(e.t.desc)
            .limit(count)
            .asList();
    }
}
